<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-page-container">
  <a [routerLink]="['..']" class="gv-back-link"><small><< Back to reporters</small></a>
  <h1>{{reporter.name}}</h1>
  <div class="reporter-content">
    <form #reporterForm="ngForm" fxLayout="column" fxFlex="70" >
      <div class="gv-form-section" fxLayout="column">
        <div fxFlex fxLayoutAlign="end">
          <mat-slide-toggle matTooltip="{{reporter.enabled ? 'Disable reporter ?' : 'Enable reporter ?'}}"
                            (change)="enableReporter($event)"
                            [checked]="reporter.enabled"></mat-slide-toggle>
        </div>
        <div *ngIf="createMode">
          <mat-form-field appearance="outline" floatLabel="always">
            <mat-label>Reporter Type</mat-label>
            <mat-select [(value)]="reporter.type" name="type" (selectionChange)="onReporterTypeChanged($event)" [(ngModel)]="reporter.type">
              <mat-option *ngFor="let plugin of plugins" [value]="plugin.id" required>
                {{labelFor(plugin.id)}}
              </mat-option>
            </mat-select>
            <mat-hint>The type of reporter.</mat-hint>
          </mat-form-field>
        </div>
        <div>
          <mat-form-field appearance="outline" floatLabel="always">
            <mat-label>Name</mat-label>
            <input matInput type="text" placeholder="Name" name="name" [(ngModel)]="reporter.name" 
            (input)="validateName()" required>
            <mat-hint>A name for your reporter.</mat-hint>
          </mat-form-field>
        </div>
      </div>

      <div class="gv-form-section">
        <div class="gv-form-section-title">
          <h5>Configuration</h5>
          <mat-divider></mat-divider>
        </div>
        <div>
          <reporter-form *ngIf="reporter" [reporterConfiguration]="reporterConfiguration" [reporterSchema]="reporterSchema" (configurationCompleted)="enableReporterUpdate($event)"></reporter-form>
        </div>
      </div>

      <div fxLayout="row">
        <button mat-raised-button [disabled]="((!reporterForm.valid || reporterForm.pristine) && (!configurationIsValid || !hasName || configurationPristine) && !formChanged)" (click)="save()">SAVE</button>
      </div>
      <ng-template [ngIf]="!createMode"> <!-- use ng-template otherwise *ngIf can't be used into the div -->
        <div *hasPermission="['domain_reporter_delete']" class="gv-page-delete-zone" fxLayout="column">
          <p>This operation cannot be undone.</p>
          Reporter will be deleted.
          <div class="gv-page-delete-button" fxLayout="row">
            <span fxFlex></span>
            <button [disabled]="isDefaultReporter()" mat-raised-button color="warn" (click)="delete($event)">DELETE</button>
          </div>
        </div>
      </ng-template>
    </form>
    <div class="gv-page-description" fxFlex>
      <h3>Reporter</h3>
      <div class="gv-page-description-content">
        <p>
          A reporter is used by the platform to report many types of event (e.g Audit logs).
        </p>
      </div>
    </div>
  </div>
</div>
